﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using UnityEngine.Networking;
using MiniJSON;

/**
    文件处理类
 */
public class GetDataFromFile{

    private static GetDataFromFile instance;

    public static GetDataFromFile Instance{
        get{
            if(instance == null){
                instance = new GetDataFromFile();
            }
            return instance;
        }
        private set{}
    }

    
    public Dictionary<string, object> GetStreamingAssetsJsonData(string path){
        string filePath = Path.Combine(Application.streamingAssetsPath, path);
        string result = "";

        if(filePath.Contains("://")){
            UnityWebRequest www = UnityWebRequest.Get(filePath);
            www.SendWebRequest();
            while(www.isDone){ }
            result = www.downloadHandler.text;
            Debug.LogError(result);
        }else{
            result = File.ReadAllText(filePath);
        }

        Dictionary<string, object> dic = Json.Deserialize(result) as Dictionary<string, object>;

        return dic;
    }

    ///获取
    public string ReadFile(string path){
        string filePath = Path.Combine(Application.streamingAssetsPath, path);
        string result = "";

        if(filePath.Contains("://")){
            UnityWebRequest www = UnityWebRequest.Get(filePath);
            www.SendWebRequest();
            while(www.isDone){ }
            result = www.downloadHandler.text;
        }else{
            result = File.ReadAllText(filePath);
        }

        return result;
    }
	
}
